// ml278
// Copyright (C) 2008 Bjørn Øivind Bjørnsen, Aleksander Gjermundsen, Knut Andre Tidemann, Thomas Hansen
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#ifndef __PLANETARYATMOSPHERERENDERABLE_H_
#define __PLANETARYATMOSPHERERENDERABLE_H_

#include <OgreMaterialManager.h>
#include <OgreRenderable.h>
#include <OgreMovableObject.h>

#define LOD_MAX 0
#define LOD_MID 1
#define LOD_MIN 2

namespace Ogre
{
	class PlanetarySceneManager;
	class PlanetaryOptions;
	class PlanetaryAtmosphere;

	/**
	 * Implements a renderable for the atmosphere.
	 * This is basically just a cube-mapped sphere, with two LOD-levels to avoid visible tesselation in the sky.
	 * Pretty much the same layout as PlanetaryZone.
	*/
	class PlanetaryAtmosphereRenderable : public Renderable, public MovableObject
	{
		public:
			PlanetaryAtmosphereRenderable(PlanetarySceneManager *mgr);
			~PlanetaryAtmosphereRenderable();

			void init(unsigned int zone, PlanetaryAtmosphere *parent);
			void load();
			void unload();
			void uninit();

			inline void setMaterial(String matname)
			{
				mMaterial = MaterialManager::getSingletonPtr()->getByName(matname);
			}

			// MovableObject overridden methods
			virtual void _notifyCurrentCamera(Camera* cam);
			virtual void _notifyAttached(Node* parent, bool isTagPoint = false);

			virtual void _updateRenderQueue(RenderQueue* queue);
			virtual void getRenderOperation(RenderOperation& rend);

			virtual const String &getMovableType() const
			{
				return mType;
			}

			virtual const AxisAlignedBox &getBoundingBox() const
			{
				return mBounds;
			}
			virtual Real getBoundingRadius() const
			{
				return mBoundingRadius;
			}


			// Renderable overridden methods
			virtual const MaterialPtr &getMaterial() const
			{
				return mMaterial;
			}

			virtual void getWorldTransforms(Matrix4 *xform) const;
			virtual const Quaternion &getWorldOrientation() const;
			virtual const Vector3 &getWorldPosition() const;
			virtual Real getSquaredViewDepth(const Camera *cam) const;
			virtual const LightList &getLights() const;

		protected:
			void loadUp(const VertexElement* poselem, uchar *pBuffer,unsigned int vertexSize, unsigned int LOD);
			void loadDown(const VertexElement* poselem, uchar *pBuffer,unsigned int vertexSize, unsigned int LOD);
			void loadFront(const VertexElement* poselem, uchar *pBuffer,unsigned int vertexSize, unsigned int LOD);
			void loadBack(const VertexElement* poselem, uchar *pBuffer,unsigned int vertexSize, unsigned int LOD);
			void loadLeft(const VertexElement* poselem, uchar *pBuffer,unsigned int vertexSize, unsigned int LOD);
			void loadRight(const VertexElement* poselem, uchar *pBuffer,unsigned int vertexSize, unsigned int LOD);

			bool mIsLoaded;
			bool mIsInit;
		
			/// Radius of the atmosphere
			Real mRadius;

			unsigned int mPlanet;

			/// index of the LOD
			unsigned int mLodIndex;


			/// which zone this renderable represents
			unsigned int mZone;

			/// Scene manager object
			PlanetarySceneManager *mSceneMgr;

			/// Options object
			PlanetaryOptions *mOptions;

			/// Atmosphere this renderable belongs to
			PlanetaryAtmosphere *mParent;

			/// Vertex data info
			VertexData *mVertices[3];

			/// Index data info
			IndexData *mIndices[3];

			/// MovableObject bounds
			AxisAlignedBox mBounds;

			/// MovableObject bounding radius
			Real mBoundingRadius;

			/// MovableObject type
			static String mType;

			/// Renderable material
			MaterialPtr mMaterial;

			/// Renderable center
			Vector3 mCenter;

			/// Whether light list need to re-calculate
			mutable bool mLightListDirty;

			/// Cached light list
			mutable LightList mLightList;
	};
}

#endif
